//
//  HomeTableViewController.swift
//  ZYBWeibo
//
//  Created by xthink4 on 16/1/11.
//  Copyright © 2016年 zyb. All rights reserved.
//

import UIKit
import SVProgressHUD
let weiboArr:NSMutableArray=NSMutableArray()
class HomeTableViewController: BaseTableViewController {
    @IBOutlet weak var titleButton: NavTitleButton!

    //打开二维码
    @IBAction func openQRcode(sender: UIButton) {
        
        let sb=UIStoryboard(name: "QRCode", bundle: nil)
        let QRcodeController=sb.instantiateInitialViewController()
        self.presentViewController(QRcodeController!, animated: true, completion: nil)
        }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupTitleButton()
        if  userLogin {
            let access_token=NSUserDefaults.standardUserDefaults().objectForKey("access_token") as! String
            let paras:Dictionary<String,AnyObject>=["access_token":access_token]
            //下载微博数据
            StatuseModel.DownLoadWeiBoData(paras, completion: { (arr) -> Void in
                SVProgressHUD.dismiss()
                let dataArr:NSArray=arr!
                weiboArr.addObjectsFromArray(dataArr as [AnyObject])
                self.tableView.reloadData()
            })
           
        }
       
    }
  ///设置按钮标题
    private func setupTitleButton(){
        let defaults=NSUserDefaults.standardUserDefaults()
        let userName=defaults.objectForKey("userName") as? String
        
        if userName==nil{
            titleButton.setImage(nil, forState: UIControlState.Normal)
        }else{
            titleButton.setTitle(userName!+" ", forState: UIControlState.Normal)
        }
        titleButton.titleLabel!.text=userName
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        visitorView?.setupInfo("visitordiscover_feed_image_house", title: "登陆后，最新，最热微博尽在掌握，不再会与事实流失擦肩而过了。",isHome: true)

    }
    // 动画转场代理
    let animatorDelegate = PopoverAnimator()
    
    // 用 stroyboard 连线跳转页面，一定会执行的代码
    // 做跳转衔接过程中的准备工作
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        if segue.identifier == "homePopoerView" {
            let popVC = segue.destinationViewController as! PopoverViewController
            
            /**
            要实现自定义Modal转场
            1. 设置转场代理
            返回 UIPresentationController，由 该控制器，负责 Modal 界面的处理
            2. 指定Modal的展现方式是自定义
            */
            // transition 转场，从一个界面跳到另外一个界面
            popVC.transitioningDelegate = animatorDelegate
            // 设置展现的大小
            let x = (self.view.bounds.width - 200) * 0.5
            animatorDelegate.presentedFrame = CGRectMake(x, 56, 200, 240)
            // 设置展现方式
            popVC.modalPresentationStyle = UIModalPresentationStyle.Custom
        }
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return weiboArr.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let id="HomeCell"
        var cell:HomeCellTableViewCell! = tableView.dequeueReusableCellWithIdentifier(id) as! HomeCellTableViewCell
        if cell == nil{
             cell=HomeCellTableViewCell(style: UITableViewCellStyle.Default, reuseIdentifier: id)
        }
        let weibo:StatuseModel=weiboArr.objectAtIndex(indexPath.row) as! StatuseModel
        cell.statuse=weibo
        return cell
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return 300
    }

  }
